package cn.sprouting.tool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.sprouting.model.Area;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：LX
 * @ Date 创建时间： 2021/10/26. 9:10
 * 地点：长沙麓谷
 * 目的: 随机生成地区信息
 * 备注说明： 街道的数据采集的是2020年的，但这一部分数据一般变动比较多，故而，这里不建议用到街道级别，一般行政区划只到省市区县，不到街道，根据
 * 自己的需求来采集相应的数据。
 */
public class RandomAreaTool {

    /**
     * 道路名称中常见的方向
     */
    private static List<String> directionList = CollUtil.newArrayList("东", "西", "南", "北", "中");


    /**
     * 生成指定数量的地区信息，包含省市区县街道以及详细地址
     * @param num 生成数量
     * @return
     */
    public static List<Area> getArea(Integer num){
        return getArea(num, null, null, null);
    }

    /**
     * 随机生成一个地区信息，包含省市区县街道以及详细地址
     * @return
     */
    public static Area getArea(){
        List<Area> areaList = getArea(1, null, null, null);
        if (areaList == null){
            return null;
        }
        return areaList.get(0);
    }

    /**
     * 随机生成一个地区信息，包含省市区县街道以及详细地址
     * @param num 生成数量
     * @param provinceCodeList 是否指定生成指定省的地区信息，注意，省市代码长度，可以为null
     * @param cityCodeList 是否指定生成指定市区的地区信息，注意，省市代码长度，可以为null
     * @param countyCodeList 是否指定生成指定县的地区信息，注意，省市代码长度，可以为null
     * @return
     */
    public static List<Area> getArea(Integer num, List<String> provinceCodeList, List<String> cityCodeList, List<String> countyCodeList) {
        String areaCsv = ResourceUtil.readUtf8Str("area.txt");
        String[] areaArray = areaCsv.split("\r\n");

        List<Area> areaList = new ArrayList<>();
        for (String area : areaArray){
            ArrayList<String> row = CollUtil.newArrayList(area.split(","));

            Area areaModel = new Area();
            // 省
            areaModel.setProvinceCode(row.get(0));
            areaModel.setProvince(row.get(1));
            // 市
            areaModel.setCityCode(row.get(2));
            areaModel.setCity(row.get(3));
            // 区县
            areaModel.setCountyCode(row.get(4));
            areaModel.setCounty(row.get(5));
            // 街道
            areaModel.setStreetCode(row.get(6));
            areaModel.setStreet(row.get(7));

            areaList.add(areaModel);
        }

        // 过滤出要随机的地区
        if (provinceCodeList != null && provinceCodeList.size() > 0){
            areaList = areaList.stream().filter(obj -> provinceCodeList.contains(obj.getProvinceCode())).collect(Collectors.toList());
        }
        if (areaList.size() == 0){
            return null;
        }
        if (cityCodeList != null && cityCodeList.size() > 0){
            areaList = areaList.stream().filter(obj -> cityCodeList.contains(obj.getCityCode())).collect(Collectors.toList());
        }
        if (areaList.size() == 0){
            return null;
        }
        if (countyCodeList != null && countyCodeList.size() > 0){
            areaList = areaList.stream().filter(obj -> countyCodeList.contains(obj.getCountyCode())).collect(Collectors.toList());
        }
        if (areaList.size() == 0){
            return null;
        }

        // 生成详细地址
        String addressWordCnTxt = ResourceUtil.readUtf8Str("addressWordCn.txt");
        ArrayList<String> addressWordCnTxtList = CollUtil.newArrayList(addressWordCnTxt.split("\r\n"));

        // 小区
        String communityNameTxt = ResourceUtil.readUtf8Str("communityName.txt");
        ArrayList<String> communityNameTxtList = CollUtil.newArrayList(communityNameTxt.split("\r\n"));
        // 小区后缀
        String communitySuffixTxt = ResourceUtil.readUtf8Str("communitySuffix.txt");
        ArrayList<String> communitySuffixTxtList = CollUtil.newArrayList(communitySuffixTxt.split("\r\n"));

        // 随机数量
        if (num == null || num < 1){
            num = 1;
        }
        List<Area> randomEleList = RandomUtil.randomEleList(areaList, num);
        for (Area area : randomEleList){
            // 省市区县
            String prefix = area.getProvince() + area.getCity() + area.getCounty() + area.getStreet();
            if (area.getStreet().endsWith("乡")){
                // 乡村地址
                String village = StrUtil.join("", RandomUtil.randomEleList(addressWordCnTxtList, 2)) + "村";
                String rural = StrUtil.join("", RandomUtil.randomEleList(addressWordCnTxtList, 2)) + "组";
                String number = RandomUtil.randomInt(1, 100) + "号";

                area.setSpecific(village + rural + number);
                area.setAddressDetail(prefix + area.getSpecific());

            } else {
                // 城镇地址
                String rural = StrUtil.join("", RandomUtil.randomEleList(addressWordCnTxtList, 2)) + RandomUtil.randomEle(directionList);
                // 小区名字
                String community = RandomUtil.randomEle(communityNameTxtList) + RandomUtil.randomEle(communitySuffixTxtList);
                String building = RandomUtil.randomInt(1, 20) + "栋";
                String unit = RandomUtil.randomInt(1, 5) + "单元";
                String room = String.format("%02d", RandomUtil.randomInt(1, 31)) + String.format("%02d", RandomUtil.randomInt(1, 5)) + "房";

                area.setSpecific(rural + community + building + unit + room);
                area.setAddressDetail(prefix + area.getSpecific());
            }
        }

        return randomEleList;
    }

}
